#
# Filename: searchPathLib.py
# $Id: searchPathLib.py,v 1.2 1999/08/13 12:39:57 toni Exp $
#
# provides functionality to use and manipulate the
# python moduules search path in a SNiFF+ environment
#
# Author: Uwe Stieber
# Copyright (c) 1998, 1999 TakeFive Software, Salzburg
#

import os
import sys

# some global variables
SNiFF_UserProfileDir = ""		# current local user SNiFF+ profile dir
SNIFF_DIR = ""					# current SNiFF+ installation dir
alreadyLookedForProfileDir = 0	# module already tried to calculate SNiFF_UserProfileDir

# extends the module search path for SNiFF+ depend on 'aPath'
# 'aPath' must be relative under $SNIFF_DIR) !!!!
#
#		append '$SNIFF_DIR/$aPath' and
#		[$SNIFF_RESOURCE_DIR | $SNIFF_DIR/Profiles/$USERNAME]/$aPath on WINDOWS
#		or $SNIFF_RESOURCE_DIR or $HOME/.sniffrc on UNIX
#
def extendModulePath(aPath):
	# append '$SNIFF_DIR/$aPath' and
	# [$SNIFF_RESOURCE_DIR | $SNIFF_DIR/Profiles/$USERNAME]/$aPath or
	# [$SNIFF_RESOURCE_DIR | $HOME/.sniffrc]/$aPath
	#
	# check the environment variable SNIFF_DIR
	if os.environ.has_key('SNIFF_DIR'):
		sniffSystemPath=os.environ['SNIFF_DIR']
	else:
		# stop with an error
		print "(searchPathLib) Cannot find environment variable @BSNIFF_DIR@B ! @rextendModulePath failed !@x"
		return 1
	# ok, SNIFF_DIR we have, but what about SNIFF_RESOURCE_DIR ?
	# Note: if SNIFF_RESOURCE_DIR is absent, we use by default
	# $SNIFF_DIR/Profiles/$USERNAME !
	privateWorkPath=""
	if os.environ.has_key('SNIFF_RESOURCE_DIR'):
		# using $SNIFF_RESOURCE_DIR
		privateWorkPath=os.environ['SNIFF_RESOURCE_DIR']
	else:
		# might be with lower case 'i'
		if os.environ.has_key('SNiFF_RESOURCE_DIR'):
			# using $SNiFF_RESOURCE_DIR
			privateWorkPath=os.environ['SNiFF_RESOURCE_DIR']
		else:
			if sys.platform == "win32":
				# platform is WINDOWS
				# using $SNIFF_DIR/Profiles/$USERNAME
				if os.environ.has_key('USERNAME'):
					# build privateWorkPath
					privateWorkPath=os.path.join(sniffSystemPath, 'Profiles', os.environ['USERNAME'])
				else:
					# oops, there is no username !!
					# has to be more flexible, because Win95/Win98 doesn't have
					# this environment variable :-(
					# set privateWorkPath to empty
					privateWorkPath=""
			else:
				# platform is UNIX
				# using $HOME/.sniffrc
				if os.environ.has_key('HOME'):
					# build privateWorkPath
					privateWorkPath=os.path.join(os.environ['HOME'], ".sniffrc")
				else:
					# oops, there is no HOME !!
					# set privateWorkPath to empty
					privateWorkPath = ""
	# save current local users profile dir
	global SNiFF_UserProfileDir
	global SNIFF_DIR
	global alreadyLookedForProfileDir
	SNiFF_UserProfileDir = privateWorkPath
	SNIFF_DIR = sniffSystemPath
	alreadyLookedForProfileDir = 1
	# was empty path given ?
	if len(aPath) > 0:
		# check everytime, that we don't have double items in
		# the module search path
		if privateWorkPath != "":
			modulePath = os.path.normpath(os.path.join(privateWorkPath, aPath))
			if modulePath not in sys.path:
				sys.path.append(modulePath)
		modulePath = os.path.normpath(os.path.join(sniffSystemPath, aPath))
		if modulePath not in sys.path:
			# append $SNIFF_DIR/$aPath
			sys.path.append(modulePath)

def getUsersProfileDir():
	# SNiFF_UserProfileDir valid ?
	if alreadyLookedForProfileDir == 0:
		extendModulePath("")
	return SNiFF_UserProfileDir

def getSniffDir():
	# SNIFF_DIR valid ?
	if alreadyLookedForProfileDir == 0:
		extendModulePath("")
	return SNIFF_DIR

